//
//  JourneryNetHandler.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "JourneryNetHandler.h"
#import "AFNetworking.h"

@implementation JourneryNetHandler


+ (void)getDataWithUrl:(NSString *)str completion:(void (^)(NSData *))block
{
    
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [cookieJar cookies]) {
        NSLog(@"%@", cookie);
    }
    
    NSDictionary *prdic = [NSDictionary dictionaryWithObjectsAndKeys:@"token",NSHTTPCookieValue,
                           @"sso_tk",NSHTTPCookieName,
                           @"/",NSHTTPCookiePath,
                           @"域名",NSHTTPCookieDomain, nil];
    
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:prdic];
    
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    
    
//    AFNetworkReachabilityManager *ma = [AFNetworkReachabilityManager ma ]
    
   NSString *URLTmp = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLTmp]];
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

    
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSString *requestTmp = [NSString stringWithString:operation.responseString];
        
        NSData *data = [[NSData alloc]initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
 
        block (data);
//        NSLog(@"!!!!!resultDic = %@",[resultDic objectForKey:@"elements"]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"Faukure : %@", error);
    }];
    
    [operation start];
}


#pragma mark cookie 调研

- (void)cookit
{
    //查看cookit
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [cookieJar cookies]) {
        NSLog(@"%@", cookie);
    }
    
    
    //设置指定的cookie
//    NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
//    [cookieProperties setObject:@"username" forKey:NSHTTPCookieName];
//    [cookieProperties setObject:@"rainbird" forKey:NSHTTPCookieValue];
//    [cookieProperties setObject:@"cnrainbird.com" forKey:NSHTTPCookieDomain];
//    [cookieProperties setObject:@"cnrainbird.com" forKey:NSHTTPCookieOriginURL];
//    [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
//    [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
//    
//    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
//    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
//    
}

- (void)cookitUse
{
    
    //Create a cookie
    NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
    [properties setValue:[@"Test Value" encodedCookieValue] forKey:NSHTTPCookieValue];
    [properties setValue:@"ASIHTTPRequestTestCookie" forKey:NSHTTPCookieName];
    [properties setValue:@".allseeing-i.com" forKey:NSHTTPCookieDomain];
    [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
    [properties setValue:@"/asi-http-request/tests" forKey:NSHTTPCookiePath];
    NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
    
    //This url will return the value of the 'ASIHTTPRequestTestCookie' cookie
//   NSURL *url = [NSURL URLWithString:@"http://api.breadtrip.com/destination/index_places/7/"];
//    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
//    
//    [request setUseCookiePersistence:NO];
//    [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];
//    [request startSynchronous];
    NSString *str = @"http://api.breadtrip.com/destination/index_places/7/";
    AFHTTPRequestOperationManager *manageCookit = [AFHTTPRequestOperationManager manager];
    [manageCookit GET:str parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
    //Should be: I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie'
//    NSLog(@"%@",[request responseString]);
}

//-(NSMutableArray*)retrunCookies{
//    NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
//    [properties setValue:[LoginViewController getLanguageType:loginInfo.lang] forKey:NSHTTPCookieValue];
//    [properties setValue:@"BENGGURU.GAIA.CULTURE_CODE" forKey:NSHTTPCookieName];
//    [properties setValue:@"" forKey:NSHTTPCookieDomain];
//    [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
//    [properties setValue:@"" forKey:NSHTTPCookiePath];
//    NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
//    return [NSMutableArray arrayWithObject:cookie];
//}
@end
